<html>
<head><meta charset="utf-8"><title>c2rust and Unique references · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html">c2rust and Unique references</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="187764168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187764168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187764168">(Feb 09 2020 at 16:22)</a>:</h4>
<p>Hi I've been playing with c2rust all day, which worked pretty awesome. but then I ran Miri(<span class="user-mention" data-user-id="120791">@RalfJ</span> ) and saw <code>trying to reborrow for SharedReadWrite, but parent tag &lt;untagged&gt; does not have an appropriate item in the borrow stack</code> <br>
when I realized that this is probably because rust doesn't allow 2 mutable(unique) references to the same object, so a common C pattern like:</p>
<div class="codehilite"><pre><span></span>multiply(result: *mut T, a: *mut T, b: *mut T);
multiply(&amp;mut a, &amp;mut a, &amp;mut b);
</pre></div>


<p>is just not allowed in rust if <code>result</code> and <code>a</code> are deferenced together.  ( while writing started to wonder if it's possible to use<code>ptr::read</code> and <code>ptr:write</code> for that)</p>



<a name="187764182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187764182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187764182">(Feb 09 2020 at 16:23)</a>:</h4>
<p>Thinking about it more, read+write isn't enough, sometimes you want to access fields and that's not possible without creating a reference</p>



<a name="187764570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187764570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187764570">(Feb 09 2020 at 16:34)</a>:</h4>
<p>The language feature for this not in stable Rust yet, so it would be unreasonable to expect c2rust to use it, but the Right Solution (eventually) is to never create references, always work with raw pointers, using <code>&amp;mut raw</code> / <code>&amp;const raw</code> for operations such as taking pointers and projecting through pointers.</p>



<a name="187765064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187765064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187765064">(Feb 09 2020 at 16:50)</a>:</h4>
<p>Right I've seen people use it. <br>
one thing I don't like is that it makes it harder to understand what the heck a dereference is.<br>
ie <code>let a:&amp;raw T = &amp;raw *t;</code> like is this safe or unsafe? what did it dereference to? can t be null/dangling?</p>



<a name="187766659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187766659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187766659">(Feb 09 2020 at 17:45)</a>:</h4>
<p>it's unsafe if you try to write it outside of an unsafe block and get told you can't.</p>



<a name="187766720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187766720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187766720">(Feb 09 2020 at 17:47)</a>:</h4>
<p>More helpfully, it depends on what type <code>t</code> is.</p>
<p>if t is a reference it's safe (and will never dangle of course)<br>
if t is a raw pointer it's unsafe (and yes it might be null or dangle)</p>



<a name="187800447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187800447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187800447">(Feb 10 2020 at 09:38)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> So you're saying that <code>let t: *const T; let r = &amp;raw *t</code> requires that t points to a valid T just like <code>&amp;*t</code> requires? the only difference is that it doesn't create a shared reference?</p>



<a name="187830044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187830044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187830044">(Feb 10 2020 at 16:12)</a>:</h4>
<p>My understanding is that you'd be able to write that and it would create a raw pointer <code>r</code> based on the raw pointer <code>t</code>, in this case <code>r</code> would just end up pointing to what <code>t</code> is pointing to.</p>
<p>However I think that the raw syntax isn't even implemented in Nightly yet, so I don't want to get too carried away with fine details here, because they might change during the implementation and stabilization.</p>



<a name="187839738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187839738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187839738">(Feb 10 2020 at 17:55)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> do you have a minimal example that runs on the playground? that makes it easier to figure out what is going on</p>



<a name="187841950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187841950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187841950">(Feb 10 2020 at 18:18)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> So you're saying that <code>let t: *const T; let r = &amp;raw *t</code> requires that t points to a valid T just like <code>&amp;*t</code> requires? the only difference is that it doesn't create a shared reference?</p>
</blockquote>
<p>for now, yes</p>



<a name="187841977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187841977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187841977">(Feb 10 2020 at 18:19)</a>:</h4>
<p>not creating a shared ref means it doesnt have to be readonly, etc</p>



<a name="187841993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187841993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187841993">(Feb 10 2020 at 18:19)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> <code>&amp;raw</code> is implemented in nightly :)</p>



<a name="187842082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842082">(Feb 10 2020 at 18:20)</a>:</h4>
<p>in the future we probably will want to relax the rules around deref so that <code>&amp;raw (*var).field</code> doesnt require anything to be a valid instance of anything</p>



<a name="187842088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842088">(Feb 10 2020 at 18:20)</a>:</h4>
<p>ah excellent!</p>



<a name="187842115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842115">(Feb 10 2020 at 18:20)</a>:</h4>
<p>but I am not sure yet what the most elegant way is to state this...</p>



<a name="187842156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842156">(Feb 10 2020 at 18:21)</a>:</h4>
<p>seems easy if you don't run a deref impl</p>



<a name="187842165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842165">(Feb 10 2020 at 18:21)</a>:</h4>
<p>and not easy otherwise</p>



<a name="187842294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842294">(Feb 10 2020 at 18:22)</a>:</h4>
<p>well we still want <code>&amp;(*raw).field</code> to require validity... maybe? or not?</p>



<a name="187842315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842315">(Feb 10 2020 at 18:22)</a>:</h4>
<p>having deref semantics depend on the surrounding <code>&amp;</code> vs <code>&amp;raw</code> seems really ugly though</p>



<a name="187842349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842349">(Feb 10 2020 at 18:23)</a>:</h4>
<p>also I am thinking on the MIR level, there is no deref impl</p>



<a name="187842354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842354">(Feb 10 2020 at 18:23)</a>:</h4>
<p>those are already lowered to fn calls</p>



<a name="187842521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842521">(Feb 10 2020 at 18:25)</a>:</h4>
<p>well if <code>&amp;(*raw).field</code> makes <code>&amp;F</code> it has to point to valid since all references must. If it makes <code>*const F</code> then it doesn't have to point to anything.</p>



<a name="187842558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842558">(Feb 10 2020 at 18:25)</a>:</h4>
<p>so <code>&amp;raw (*x).field</code> can point to invalid</p>



<a name="187842617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842617">(Feb 10 2020 at 18:26)</a>:</h4>
<p>but <code>&amp;(*x).field</code> must be valid</p>



<a name="187842658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842658">(Feb 10 2020 at 18:26)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> we were talking about the deref, not the ref, I thought</p>



<a name="187842720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842720">(Feb 10 2020 at 18:27)</a>:</h4>
<p>so, the <code>*x</code> subexpression of this, what exactly are its proof obligations? <code>x</code> must be non-null and aligned for sure. and probably also dereferencable. and maybe point to a valid <code>T</code>?</p>



<a name="187842740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842740">(Feb 10 2020 at 18:27)</a>:</h4>
<blockquote>
<p>well we still want <code>&amp;(*raw).field</code> to require validity... maybe? or not?</p>
</blockquote>
<p>i was responding to this thought here. Im saying that we shoud want that to require validity there</p>



<a name="187842861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842861">(Feb 10 2020 at 18:28)</a>:</h4>
<p>well... we might <em>not</em> want to require all references to point to a valid <code>T</code> even when unused. and if we go down that route, I dont think there is anything in that expression that makes such an obligation arise.</p>



<a name="187842919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187842919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187842919">(Feb 10 2020 at 18:29)</a>:</h4>
<p>as to *x on its own, honestly the point of a lot of this is for maybe uninit, so reading it as just *x is probably the wrong design because then you couldn't initialize fields individually of a MaybeUninit (if i follow you correctly)</p>



<a name="187843323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187843323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187843323">(Feb 10 2020 at 18:34)</a>:</h4>
<p>the behavior of a compung expression like <code>&amp;raw (*x).field</code> ought to be defined by defining all of its pieces separately</p>



<a name="187843343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187843343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187843343">(Feb 10 2020 at 18:34)</a>:</h4>
<p>if we cant do that we'll just end up with a mess</p>



<a name="187843411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187843411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187843411">(Feb 10 2020 at 18:35)</a>:</h4>
<p>though to do this we will have to be more explicit about place-to-value coercion... let's call it <code>load</code></p>



<a name="187843431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187843431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187843431">(Feb 10 2020 at 18:35)</a>:</h4>
<p>which there are none here (well, <code>x</code> is really <code>load(x)</code> but that's trivial), but in <code>let val = *x</code> there's one: <code>let val = load(*x)</code></p>



<a name="187845297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187845297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187845297">(Feb 10 2020 at 18:46)</a>:</h4>
<p>if <code>*x</code> doesn't need to be valid <em>until you load it</em> that probably works out.</p>
<p>Then <code>(*x).field</code> is<br>
"follow the address of X to a place, then offset to the sub-place of the particular field"</p>



<a name="187895688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187895688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187895688">(Feb 11 2020 at 09:31)</a>:</h4>
<blockquote>
<p>the behavior of a compung expression like <code>&amp;raw (*x).field</code> ought to be defined by defining all of its pieces separately</p>
</blockquote>
<p>I 100% agree. that was my main complaint. the behavior of <code>*x</code> cannot(imo) be defined by if it has <code>&amp;</code> or <code>&amp;raw</code> before it.<br>
maybe we can change the syntax though? and not have the <code>*</code> operation when creating raw "references". not sure how it would look though</p>
<p>Personally I prefer to stick with the common well known "deference requires the pointer to be a valid aligned pointer to an existing object" and just not use dereferencing in raw references</p>



<a name="187897577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187897577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187897577">(Feb 11 2020 at 09:59)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> do you have a minimal example that runs on the playground? that makes it easier to figure out what is going on</p>
</blockquote>
<p>I was able to get it down to this minimal example: <a href="https://play.rust-lang.org/?edition=2018&amp;gist=ee4b825dc44c918c6fc8c9fb58810bfa" target="_blank" title="https://play.rust-lang.org/?edition=2018&amp;gist=ee4b825dc44c918c6fc8c9fb58810bfa">https://play.rust-lang.org/?edition=2018&amp;gist=ee4b825dc44c918c6fc8c9fb58810bfa</a> (obviously the real code is more contrived but it all starts because of 2 mutable references to the same place)</p>
<p>I can try going over c2rust output and replace all unneeded mutable references with shared references, but idk if it will be possible in all places where 2 mutable references to the same place are created.</p>



<a name="187930185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187930185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187930185">(Feb 11 2020 at 16:56)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> Requiring all pointers to be aligned and valid is just unacceptable, because then there is no way to get to fields.</p>



<a name="187930822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187930822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187930822">(Feb 11 2020 at 17:03)</a>:</h4>
<p>Giving two different interpretations to place expressions depending on what context they are used is not a very elegant formalism, but since this context always comes from the immediate syntactic surroundings it does not actually cause any technical problems. While the potential for confusion when reading code is important, inventing a whole second set of syntax doesn't sound like it's an obvious improvement, especially given that expressions like <code>(*raw_ptr).field</code> are already wide-spread and should presumably have just as few restrictions on <code>raw_ptr</code> as whatever new syntax gets introduced.</p>



<a name="187931554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187931554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187931554">(Feb 11 2020 at 17:10)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> they can. Just not when dereferencing.<br>
My point is that dereferencing a raw pointer and creating a reference should be a different operation than creating a "raw reference"</p>
<p>(honestly I'm not quite sure why we to add raw references and we can't just implement all the needed functionality on pointers)</p>



<a name="187932751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187932751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187932751">(Feb 11 2020 at 17:22)</a>:</h4>
<p>There's no "raw references". There's just an operator to create a raw pointer to a memory location, immediately, without involving a reference at all. Describing the memory location in question can involve many nested operations, not just dereferencing, regardless of what you wind up doing with the memory location (taking a reference, taking a raw pointer, reading data directly, writing data directly, etc.). For example, consider <code>&amp;mut raw (*ptr).data[1]</code> where <code>ptr: *mut Foo</code> with <code>struct Foo { header: u16, data: [u64; 8] }</code>.</p>



<a name="187933431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187933431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187933431">(Feb 11 2020 at 17:29)</a>:</h4>
<p>Yes, to be clear, <code>&amp;raw</code> isn't some new data type, it's just the way to immediately make *const T instead of making &amp;T and then having it be coerced to *const T by usage.</p>



<a name="187994420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187994420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187994420">(Feb 12 2020 at 09:40)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> Why not allow doing <code>ptr.field</code>, which will create a raw pointer to the field?</p>



<a name="187994631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187994631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187994631">(Feb 12 2020 at 09:44)</a>:</h4>
<p>This is a pretty wide design space of what could have happened.</p>
<p>Long story short, we got <code>&amp;raw</code> and that's what we got.</p>



<a name="187994687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/187994687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#187994687">(Feb 12 2020 at 09:44)</a>:</h4>
<p>Yeah I guess it's too late to do any major change here</p>



<a name="188011393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188011393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188011393">(Feb 12 2020 at 13:56)</a>:</h4>
<p>Not too late, just has a higher hill to climb.</p>



<a name="188011410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188011410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188011410">(Feb 12 2020 at 13:56)</a>:</h4>
<p>It's not stable yet.</p>



<a name="188088369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188088369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188088369">(Feb 13 2020 at 08:58)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@Hanna Kruppe</span> I think we can and should avoid both context-dependent interpretation of place expressions and separate syntax</p>



<a name="188088389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188088389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188088389">(Feb 13 2020 at 08:58)</a>:</h4>
<p>and it's not like that's impossible, it's just that there's a bunch of choices and no clear winner</p>



<a name="188088428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188088428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188088428">(Feb 13 2020 at 08:59)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> the main counterexample for that was that it is very inconsistent with the behavior of <code>.field</code> on anything else. the same syntax shouldnt have to widely different behaviors.</p>



<a name="188088916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188088916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188088916">(Feb 13 2020 at 09:06)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I think as a user of the language I feel it makes more sense to do that then to have the behavior through the <code>&amp;</code> operator.<br>
I'm already not quite sure what happens when I do something like<code>&amp;mut (*(*(*p).a).b)[0]</code> is there any shared reference produced in the middle? Does it produce unique references at each deref? Neither?</p>



<a name="188089614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188089614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188089614">(Feb 13 2020 at 09:15)</a>:</h4>
<p>not sure what you mean by "behavior through the <code>&amp;</code> op"</p>



<a name="188089654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188089654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188089654">(Feb 13 2020 at 09:16)</a>:</h4>
<p>we now have two <code>&amp;</code> ops that have different behavior</p>



<a name="188089666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188089666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188089666">(Feb 13 2020 at 09:16)</a>:</h4>
<p>but it is only locally different, the p,lace below them still gets evaluated exactly the same</p>



<a name="188094036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188094036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188094036">(Feb 13 2020 at 10:31)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I'd like to hear what choices you see for having a single semantics for place expressions. It's not that I can't imagine any such design, but there's some UB in place expression evaluation that we'd (presumably) want to keep for most contexts but may want to remove from raw pointer projections (most importantly, the requirement that the base pointer is not dangling).</p>



<a name="188094467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188094467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188094467">(Feb 13 2020 at 10:38)</a>:</h4>
<p>Hm, I think I was imagining we'd keep <code>inbounds</code> because, as you kept pointing out, it's important to LLVM to know that the arithmetic is non-wrapping.</p>



<a name="188094496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188094496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188094496">(Feb 13 2020 at 10:39)</a>:</h4>
<p>I have hope for a future where "non-wrapping" and "base pointer not dangling" are decoupled.</p>



<a name="188153889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188153889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188153889">(Feb 13 2020 at 21:28)</a>:</h4>
<p>you think it is realistic that LLVM will adopt this? so we should design MIR semantics for such a future?</p>



<a name="188154517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188154517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188154517">(Feb 13 2020 at 21:35)</a>:</h4>
<p>I don't know. It may very well fall below the threshold of being worth proposing, hashing out. implementing, etc.<br>
I also don't think we should <em>currently</em> define our semantics in anticipation of it: it's better to remove some UB in the future than to eat the performance impact of not emitting <code>inbounds</code> for however long <code>inbounds</code> exists.</p>
<p>But I think it's definitely a possibility, and so I think it's also possible that we'd have a good reason to want multiple interpretations of place expressions eventually.</p>



<a name="188186073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188186073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188186073">(Feb 14 2020 at 08:09)</a>:</h4>
<p>I see. I am concerned about our complexity budget here, which we are already straining...</p>



<a name="188186177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188186177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188186177">(Feb 14 2020 at 08:11)</a>:</h4>
<p>but <em>if</em> we go for a "contextual" semantics, I feel we should at least do some benchmarks to see how badly we need <code>inbounds</code> for these raw ptr ops... <em>if</em> we pay the complexity price anyway, being able to say that with raw ptrs the ops are entirely safe would be quite nice</p>



<a name="188186237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/c2rust%20and%20Unique%20references/near/188186237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/c2rust.20and.20Unique.20references.html#188186237">(Feb 14 2020 at 08:12)</a>:</h4>
<p>I think an alternative to a contextual semantics might be to use the type of the ptr? so, make it depend not on <code>&amp;</code> vs <code>&amp;raw</code> furhter outside, but on whether the ptr is raw or a ref? that would be structural and not require context info</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>